{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    @file Sklearn\n",
    "    @description Decision Tree By Sklearn\n",
    "    @author Synhard\n",
    "    @tel 13001321080\n",
    "    @id 21126338\n",
    "    @email 823436512@qq.com\n",
    "    @date 2021-09-25 19:21\n",
    "    @version 1.0\n",
    "\"\"\"\n",
    "# 构建模型\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot as plt\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "import pandas as pd\n",
    "\n",
    "decisionTree = DecisionTreeClassifier()\n",
    "source = pd.read_excel(\"heart.xlsx\")\n",
    "label = source['target']\n",
    "data = source.drop('target', axis=1)\n",
    "train_X, test_X, train_y, test_y = train_test_split(data, label, random_state=3, test_size=0.3)\n",
    "\n",
    "a = source.columns[0:-1]\n",
    "# 训练数据\n",
    "decisionTree = DecisionTreeClassifier(max_depth=5, criterion=\"entropy\")\n",
    "# decisionTree = DecisionTreeClassifier(max_depth=5, criterion=\"gini\")\n",
    "decisionTree.fit(train_X, train_y)\n",
    "\n",
    "# 预测数据\n",
    "pred_y = decisionTree.predict(test_X)\n",
    "\n",
    "# 评估模型\n",
    "print(accuracy_score(test_y, pred_y))\n",
    "\n",
    "print(decisionTree.score(train_X, train_y))\n",
    "\n",
    "print(decisionTree.score(test_X, test_y))\n",
    "\n",
    "param_test = {'max_features': ['auto', 'sqrt', 'log2'],\n",
    "              'min_samples_split': list(range(2, 20)),\n",
    "              'min_samples_leaf': list(range(1, 12))\n",
    "\n",
    "              }\n",
    "\n",
    "tree_gv = GridSearchCV(estimator=decisionTree, param_grid=param_test, cv=5)\n",
    "tree_gv.fit(train_X, train_y)\n",
    "\n",
    "# 最优参数\n",
    "\n",
    "# 预测数据\n",
    "pred_y = tree_gv.predict(test_X)\n",
    "print(classification_report(test_y, pred_y))\n",
    "\n",
    "# 可视化决策树\n",
    "export_graphviz(\n",
    "    decisionTree,\n",
    "    out_file=\"D:\\研一\\AI\\dt\\dt_sklearn_entropy.dot\",\n",
    "    feature_names=source.columns[0:-1],\n",
    "    rounded=True,\n",
    "    filled=True\n",
    ")\n",
    "\n",
    "# export_graphviz(\n",
    "#     decisionTree,\n",
    "#     out_file=\"D:\\研一\\AI\\dt\\dt_sklearn_gini.dot\",\n",
    "#     feature_names=source.columns[0:-1],\n",
    "#     rounded=True,\n",
    "#     filled=True\n",
    "# )\n",
    "\n",
    "fig1 = plt.figure(figsize=(3 * 5, 1 * 4))\n",
    "matrix = pd.DataFrame(confusion_matrix(test_y, pred_y))\n",
    "sns.heatmap(matrix, annot=True, cmap='OrRd')\n",
    "plt.title('Confusion Matrix -- %s ')\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}